package com.cj.web.before;

import com.cj.entity.CourseUser;
import com.cj.service.CourseUserService;
import com.cj.utils.JSONUtils;
import com.cj.utils.ResultVO;
import com.cj.web.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: chenjie
 * @Date: 2021/7/15 - 07 - 15 - 20:05
 * @Description: com.cj.web.before
 */
@WebServlet("/courseUserWeb")
public class CourseUserServlet extends BaseServlet {
    CourseUserService service = new CourseUserService();
    ResultVO vo;
    public  void insertCourseUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("insertCourseUser");
        String uid = req.getParameter("uid");
        String cid = req.getParameter("cid");

        int row = service.insertCourseUser(cid,uid);
        if(row>0){

//            购买成功，跳转用户页面   只能返回uid让页面自己查，返回的数据页面无法接收
            resp.sendRedirect("http://127.0.0.1:5051/%D0%A1u%D1%A7%CF%B0/pages/course.html?uid="+uid);

        }else{
            vo = new ResultVO(500,"购买失败",null);
            JSONUtils.objToJson(vo,resp);
        }


    }


public void findPageByPayed(HttpServletRequest req,HttpServletResponse resp) throws IOException {
    String cid = req.getParameter("cid");
    String uid = req.getParameter("uid");

    CourseUser courseUser = service.findCourseUser(cid,uid);
    if (courseUser != null) {
        vo = new ResultVO(200,"已购买",null);
    }else{
        vo = new ResultVO(500,"未购买",null);
    }
    JSONUtils.objToJson(vo,resp);
}
}
